﻿using KalevaAalto.DataPart.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Diagnostics;

namespace KalevaAalto.DataPart.DataBasePart
{
    public class SqlLite : DataBaseBase, IDisposable
    {



        public string FileName { get; set; } = string.Empty;
        public string ConnectionString  => $"Data Source={FileName};";


        public override SqlSugarClient GetSqlSugarClient()
        {
            return new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = ConnectionString,
                DbType = DbType.Sqlite,
                InitKeyType = InitKeyType.Attribute
            });
        }




        public override bool Status => db is not null && db.Ado.Connection.State == System.Data.ConnectionState.Open;

        public override void Connect()
        {
            if(!string.IsNullOrEmpty(FileName) && !File.Exists(FileName))
            {
                using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
                {
                    connection.Open();
                    Trace.WriteLine($"{FileName} ： SQLite 数据库创建成功；");
                }
            }
            
        }

        public override Task ConnectAsync()
        {
            Connect();
            return Task.CompletedTask;
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        
    }
}
